📜 [專欄新文章] Gas Efficient Card Drawing in Solidity
✍️ Ping Chen
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Assign random numbers as the index of newly minted NFTs
Scenario
The fun of generative art NFT projects depends on randomness. The industry standard is “blind box”, where both the images’ serial number and the NFTs’ index are predetermined but will be shifted randomly when the selling period ends. (They call it “reveal”) This approach effectively solves the randomness issue. However, it also requires buyers to wait until the campaign terminates. What if buyers want to know the exact card right away? We’ll need a reliable onchain card drawing solution.
The creator of Astrogator🐊 isn’t a fan of blind boxes; instead, it thinks unpacking cards right after purchase is more interesting.
Spec
When initializing this NFT contract, the creator will determine the total supply of it. And there will be an iterable function that is randomly picking a number from the remaining pool. The number must be in range and must not collide with any existing ones.
Our top priority is accessibility/gas efficiency. Given that gas cost on Ethereum is damn high nowadays, we need an elegant algorithm to control gas expanse at an acceptable range.
Achieving robust randomness isn’t the primary goal here. We assume there’s no strong financial incentive to cheat, so the RNG isn’t specified. Implementers can bring their own source of randomness that they think is good enough.
Implementation
Overview
The implementation is pretty short and straightforward. Imagine there’s an array that contains all remaining(unsold) cards. When drawIndex() is called, it generates a (uniform) random seed to draw a card from the array, shortens the array, and returns the selected card.
Algorithm
Drawing X cards from a deck with the same X amount of cards is equal to shuffling the deck and dealing them sequentially. It’s not a surprise that our algorithm is similar to random shuffling, and the only difference is turning that classic algo into an interactive version.
A typical random shuffle looks like this: for an array with N elements, you randomly pick a number i in (0,N), swap array[0] and array[i], then choose another number i in (1,N), swap array[1] and array[i], and so on. Eventually, you’ll get a mathematically random array in O(N) time.
So, the concept of our random card dealing is the same. When a user mints a new card, the smart contract picks a number in the array as NFT index, then grabs a number from the tail to fill the vacancy, in order to keep the array continuous.
Tweak
Furthermore, as long as the space of the NFT index is known, we don’t need to declare/initialize an array(which is super gas-intensive). Instead, assume there’s such an array that the n-th element is n, we don’t actually initialize it (so it is an array only contains “0”) until the rule is broken.
For the convenience of explanation, let’s call that mapping cache. If cache[i] is empty, it should be interpreted as i instead of 0. On the other hand, when a number is chosen and used, we’ll need to fill it up with another unused number. An intuitive method is to pick a number from the end of the array, since the length of the array is going to decrease by 1.
By doing so, the gas cost in the worst-case scenario is bound to be constant.
Performance and limitation
Comparing with the normal ascending index NFT minting, our random NFT implementation requires two extra SSTORE and one extra SLOAD, which cost 12600 ~ 27600 (5000+20000+2600) excess gas per token minted.
Theoretically, any instantly generated onchain random number is vulnerable. We can restrict contract interaction to mitigate risk. The mitigation is far from perfect, but it is the tradeoff that we have to accept.
ping.eth
Gas Efficient Card Drawing in Solidity was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有114部Youtube影片,追蹤數超過4萬的網紅R. Y.,也在其Youtube影片中提到,Using Number 88: Gimmick Puppet of Leo's effect to Instant Win again The previous one: https://youtu.be/4QWUYBlIF2o 再做一次命運獅子特殊勝利,這樣就分別有先攻成功和後攻成功的決鬥了 之...
card number 在 IELTS Nguyễn Huyền Facebook 的最讚貼文
TỪ VỰNG IELTS - CHỦ ĐỀ THROWAWAY SOCIETY
🎯PHẦN TỪ VỰNG
- throwaway society: xã hội mà trong đó người ta sử dụng đồ đạc trong một thời gian ngắn sau đó vứt chúng đi, mua thứ đồ mới
vpurchase the latest version of things: mua phiên bản mới nhất
- encourage somebody to do something: khuyến khích ai đó làm gì
- to be in good condition and perfectly functional: trong tình trạng tốt và các chức năng hoạt động tốt
- overconsumption = excessive consumption: việc tiêu thụ quá mức
- overproduction = excessive production: việc sản xuất quá mức
- long-lived products = durable products = products that are made to last for a long period of time: những sản phẩm có tuổi thọ dài
- short-lived products = non-durable products = products that are made to last for a short period of time: những sản phẩm có tuổi thọ ngắn
- repurchase = buy again: mua lại
- repeat customers: những khách hàng sẽ mua hàng lại ở cửa hàng họ đã mua
- make a dent in your finance: làm giảm số tiền bạn đang có
- higher profit margins for manufacturers: lợi nhuận biên lớn hơn cho các nhà sản xuất
- tempt customers into buying the latest, novelty items: thu hút khách hàng mua những sản phẩm mới nhất
- disposable products: những sản phẩm dùng 1 lần rồi vứt đi
- solid waste generation rates: tỷ lệ thải ra chất thải rắn
- environmental degradation: sự suy khóa môi trường
- contaminate soil and water: làm ô nhiễm đất và nước
- increase consumer spending: tăng chi tiêu của khách hàng
- bad spending habits: thói quen tiêu xài xấu
- leftover food: đồ ăn thừa
- a new version with more features: phiên bản mới với nhiều tính năng hơn
- have a negative/detrimental/harmful effect on: có ảnh hưởng có hại lên...
- buy things that you do not really need: mua những thứ mà bạn không thực sự cần
- a waste of money: một sự lãng phí tiền của
- buy new things to relieve stress or anxiety: mua đồ mới để giải tỏa căng thẳng và lo âu
- overspend = spend beyond one's income: chi tiêu quá mức
- huge amounts of toxic waste: 1 lượng khổng lồ chất thải
- to be discharged into the environment: bị thải ra môi trường
- encourage people to reuse and recycle more: khuyến khích người dân tái chế và tái sử dụng nhiều hơn.
- get into serious debt: bị mắc nợ nghiêm trọng
🎯PHẦN BÀI MẪU
These days people in some countries are living in a “throwaway” society which means people use things in a short time then throw them away. What are its causes and what impacts can it have?
Dịch đề: Ngày nay người dân ở một số quốc gia đang sống trong một “xã hội bỏ đi” nghĩa là người ta sử dụng đồ đạc trong một thời gian ngắn sau đó vứt chúng đi. Nguyên nhân và ảnh hưởng mà xã hội này gây ra?
The topic of “throwaway society” has been discussed in recent years. In this society, people tend to throw away their old items regardless of whether or not they still work and purchase the latest version of things. This essay will look at some primary reasons for this phenomenon and the adverse impacts that it could have on individuals and the environment.
There are several causes of the throwaway society and I think advertising could be the first culprit. Advertisements encourage people to purchase goods and services that they do not really need by delivering a message that possessing the latest version of an item could improve a person’s self-image, confidence and even social status. For example, many of today’s teenagers spend nearly $1,500 buying iPhone X to show that they are wealthy and fashionable even though their old phones are still in good condition and perfectly functional. Another cause is mass production of non-durable goods which are often used once and thrown away. These products are much cheaper and more convenient than durable ones, hence, attract a large number of customers.
Individuals living in the throwaway society could suffer a great deal. They could easily get into serious debt if they get into the habit of buying the latest, novelty items. Many teens are being burdened with thousands of dollars in credit card debt for their unnecessary purchases, which could lead to depression and anxiety. Environmental degradation is another problem facing the throwaway society. Due to excessive production of disposable goods, huge amounts of hazardous waste are being discharged into the environment, which directly contaminates soil and water.
In conclusion, there are various factors leading to the advent of our throwaway society and increasing consumer debt and environmental damage are the two major problems which result from this society.
(299 words, written by Nguyen Huyen)
PHẦN VÍ DỤ: https://ielts-nguyenhuyen.com/tu-vung-ielts-chu-de-throwaway-society-dich-tu-chi-tiet/
#ieltsnguyenhuyen
card number 在 Facebook 的精選貼文
星矢冥界第二彈中獎名單
恭喜各位!
UID Facebook名 Card Number
962615004 Leong Mun Kit 10074
411431405 項歆晴 10074
237292629 Jeffery Kuo 10068
635300581 蔡尚融 10072
885635457 黃永昌 10068
神魔之塔 Tower of Saviors
#平民百姓
card number 在 R. Y. Youtube 的最佳貼文
Using Number 88: Gimmick Puppet of Leo's effect to Instant Win again
The previous one: https://youtu.be/4QWUYBlIF2o
再做一次命運獅子特殊勝利,這樣就分別有先攻成功和後攻成功的決鬥了
之前的:https://youtu.be/OZcFinC8s5k
決鬥重溫 リプレイ Duel Replay:
https://duellinks.konami.net/att/03779d011fbbad2fc8828fbf3faa19f2c27c94187e
card number 在 R. Y. Youtube 的最佳解答
Gimmick Puppet of Leo Instant Win Combo using Advanced Ritual Art, Vennu, Bright Bird of Divinity, Palace of the Elemental Lords, and Necro Fusion in the hand.
Duel Replay:
https://duellinks.konami.net/att/04ab1d4e1150ce8ce1781aaf3c666b7a15da53059a
------------------------------------------------
繁體中文版:https://youtu.be/OZcFinC8s5k
日本語版:https://youtu.be/EPULLXoPuJo
card number 在 R. Y. Youtube 的最佳貼文
A 4-Card OTK using Gateway to Chaos, Vylon Cube, Thunder Dragon, and Infernity Archfiend.
Duel Replay:
https://duellinks.konami.net/att/03c584ae4b5b7fc6fa7c9bbf0a8f615e882ee61ba1
Decklist:
https://duellinks.konami.net/att/0479b0d51292309ff37a87b47cb396eb07550dbb9a
------------------------------------------------
Skill: Not Set
Revendread Slayer ×1
Vendread Battlelord ×1
Grandsoil the Elemental Lord ×1
Umbramirage the Elemental Lord ×1
Gaia the Fierce Knight Origin ×1
Infernity Archfiend ×2
Thunder Dragon ×3
Vendread Revenants ×1
Vylon Cube ×1
Bee List Soldier ×1
Elementsaber Aina ×2
Elementsaber Makani ×2
Deskbot 001 ×1
Living Fossil ×1
The Gates of Dark World ×1
Overdone Burial ×1
Palace of the Elemental Lords ×2
Revendread Origin ×1
Gateway to Chaos ×1
Revendread Evolution ×1
Vendread Nights ×1
Blasting the Ruins ×3
Ancient Fairy Dragon ×3
Deskbot Jet ×1
Hieratic Sun Dragon Overlord of Heliopolis ×1
Mecha Phantom Beast Dracossack ×1
Number 28: Titanic Moth ×1
------------------------------------------------
繁體中文版:https://youtu.be/hU8a449yLyM
日本語版:https://youtu.be/nMazydWoDSk
card number 在 發卡行識別碼- 維基百科,自由的百科全書 的相關結果
發卡行識別碼(英語:Bank card number或Bank Identification Number,簡稱BIN),通常是指銀行卡卡號前6位、用來表示發卡銀行或機構的一套銀行卡卡號編碼。 ... <看更多>
card number 在 Test card numbers | Adyen Docs 的相關結果
The cards and payment method details on this page will not work on our live platform. Credit and debit cards. Here we provide test card numbers for the ... ... <看更多>
card number 在 Test Card Numbers - Support Guides - BlueSnap 的相關結果
Card Type Card Number Exp. Date Country/Currency Result
Amex 374245455400126 05/2023 Success
Amex 378282246310005 05/2023 Failure
China Union Pay 6250941006528599 06/2023 CN; USD Success ... <看更多>